/* JavaScript functions that are needed to navigate between different
   components on the form */

/*
 * direction -The direction to move in the fields. Codes fare the following:
 *    0-right, 1-down
 *    2-left, 3-up
 */
function getNextComponent(row, col, direction, OldGrp) {
    switch (direction) {
        case 0:    //Move right
            if (CompMap[row][col+1] && extractCalc(CompMap[row][col+1])!='c')
                return CompMap[row][col+1];
            if (CompMap[row+1][0] && extractCalc(CompMap[row][col+1])!='c')
                return CompMap[row+1][0];
            else
                return(CompMap[row][col]);
            break;
        case 1:    //Move Down
            if (CompMap[row+1][0]) {
                if (OldGrp != extractGrp(CompMap[row+1][0]))
                    return CompMap[row+1][0];
                while (!CompMap[row+1][col]) col--;
                return CompMap[row+1][col];
            } else
                return CompMap[row][col];
            break;
        case 2:    //Move left
            if (col > 0  && extractCalc(CompMap[row][col-1])!='c')
                return(CompMap[row][col-1]);
            if (row > 0) {
                //Find the last component on the new row
                for (var c=0; (CompMap[row-1][c] && extractCalc(CompMap[row-1][c])!='c'); c++);
                return(CompMap[row-1][c-1]);
            } else
                return(CompMap[row][col]);
            break;
        case 3:    //Move up
            if (row > 0) {
                if (OldGrp != extractGrp(CompMap[row-1][0]))
                    return CompMap[row-1][0];
                while (!CompMap[row-1][col]) col--;
                return CompMap[row-1][col];
            } else
                return CompMap[row][col];
            break;
    }
}
